<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button">{{this.currentCity}}</div>
      </div>
    </div>
    <div class="area">
      <div class="title border-topbottom">热门城市</div>
      <div class="button-list">
        <div class="button" v-for="item of hotCities" :key="item.id" @click="handleCity(item.name)">{{ item.name}}</div>
      </div>
    </div>
    <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
      <div class="title border-topbottom">{{key}}</div>
      <div class="item-list">
        <div class="item border-bottom" v-for="innerItem of item" :key="innerItem.id" @click="handleCity(innerItem.name)">{{innerItem.name}}</div>
      </div>
    </div>
    </div>
  </div>
</template>
<script>
import Bscroll from 'better-scroll'
import {mapState, mapMutations} from 'vuex'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  methods: {
    handleCity (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity']) // 把名为changeCity的mutation映射到当前组件名为mutationCity的方法
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  computed: {
    ...mapState({ // 映射到当前组件的计算属性currentCity中
      currentCity: 'city'
    })
  },
  watch: {
    letter () {
      if (this.letter) {
        const ele = this.$refs[ this.letter ][0]
        this.scroll.scrollToElement(ele)
      }
    }
  }
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .list
    overflow: hidden
    position: absolute
    top: 2.75 rem
    left: 0
    right: 0
    bottom: 0
    .border-topbottom
      &:before
        border-color: #ccc
      &:after
        border-color: #ccc
    .border-bottom
      &:before
        border-color: #ccc
    .title
      height: .44 rem
      line-height: .44 rem
      background: #eee
      color: #666
      padding: .1 rem
    .button-list
      padding: .1rem .3rem .1rem .1rem
      overflow: hidden
      .button
        float: left
        width: 30%
        border: .02rem solid #ccc
        padding: .1rem 0
        margin: .05rem .05rem
    .item-list
      .item
        line-height: .76rem
        text-align: left
        padding-left: .2 rem
</style>
